﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model.Public;
using Cache.Redis;
using MySql.Data.MySqlClient;
using System.Data;

namespace Model.User
{
    /// 角色表
    /// <summary>
    /// 角色表
    /// </summary>
    public class RoleData : BaseClass<RoleData>
    {
        public int UID = 0;

        public string Name = "none";

        public int Gold = 1;

        public int Food = 1;

        public int SeaMan = 1;

        public int WorkMan = 1;

        public int Diamond = 0;

        public int Level = 1;

        public int Exp = 0;

        public int Windy_dir = 1;

        public int Windy_level = 0;

        public float Windy_remain_time = 0;

        public int Windy_remain_time_start = 0;

        public int PopularSeaPointId = 0;

        public int PopularProduceId = 0;

        public int PopularProduceRate = 0; // 100 = 100%

        public float PopularRemainTime = 0;

        public int PopularRemainTimeStart = 0;

        public int RefreshNum = 0;          //任务的刷新值

        public int DataFlag = 0;

        public DateTime CreateTime;


        #region 方法

        public const string DB_Name = "Role";  //redis角色表key
        public const int MAX_GOLD_NUM = 999999999;  // TODO: 读表

        public string GetDBName()
        {
            return DB_Name + "_" + this.UID.ToString();
        }

        public void SetDBName(string uid)
        {
            this.UID = Convert.ToInt32(uid);
        }

        public int GetKey()
        {
            return this.UID;
        }

        public void SetKey(int uid)
        {
            this.UID = uid;
        }
        // gold
        public bool HasGold(int v)
        {
            return this.Gold >= v;
        }
        public bool AddGold(int v)
        {
            return glbUtil.addInt(ref this.Gold, v, MAX_GOLD_NUM);
        }
        public bool SubGold(int v)
        {
            return glbUtil.subInt(ref this.Gold, v);
        }

        /// 类to字典
        /// <summary>
        /// 类to字典
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public Dictionary<string, object> ModelChangeToDic()
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("UID", UID);
            result.Add("Name", Name);
            result.Add("Gold", Gold);
            result.Add("Food", Food);
            result.Add("SeaMan", SeaMan);
            result.Add("WorkMan", WorkMan);
            result.Add("Diamond", Diamond);
            result.Add("Level", Level);
            result.Add("Exp", Exp);
            result.Add("Windy_dir", Windy_dir);
            result.Add("Windy_level", Windy_level);
            result.Add("Windy_remain_time", Windy_remain_time);
            result.Add("Windy_remain_time_start", Windy_remain_time_start);
            result.Add("PopularSeaPointId", PopularSeaPointId);
            result.Add("PopularProduceId", PopularProduceId);
            result.Add("PopularProduceRate", PopularProduceRate);
            result.Add("PopularRemainTime", PopularRemainTime);
            result.Add("PopularRemainTimeStart", PopularRemainTimeStart);
            result.Add("RefreshNum", RefreshNum);
            result.Add("DataFlag", DataFlag);
            return result;
        }

        /// 字典to类
        /// <summary>
        /// 字典to类
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public void DicChangeToModel(Dictionary<string, object> dic)
        {
            object value = null;
            UID = dic.ContainsKey("UID") ? Convert.ToInt32(dic["UID"]) : 0;
            Name = dic.ContainsKey("Name") ? dic["Name"].ToString() : "";
            Gold = dic.ContainsKey("Gold") ? Convert.ToInt32(dic["Gold"]) : 0;
            Food = dic.ContainsKey("Food") ? Convert.ToInt32(dic["Food"]) : 0;
            SeaMan = dic.ContainsKey("SeaMan") ? Convert.ToInt32(dic["SeaMan"]) : 0;
            WorkMan = dic.ContainsKey("WorkMan") ? Convert.ToInt32(dic["WorkMan"]) : 0;
            Diamond = dic.ContainsKey("Diamond") ? Convert.ToInt32(dic["Diamond"]) : 0;
            Level = dic.ContainsKey("Level") ? Convert.ToInt32(dic["Level"]) : 0;
            Exp = dic.ContainsKey("Exp") ? Convert.ToInt32(dic["Exp"]) : 0;
            Windy_dir = dic.ContainsKey("Windy_dir") ? Convert.ToInt32(dic["Windy_dir"]) : 0;
            if (dic.TryGetValue("Windy_level", out value)) Windy_level = Convert.ToInt32(value);
            Windy_remain_time = dic.ContainsKey("Windy_remain_time") ? Convert.ToSingle(dic["Windy_remain_time"]) : 0;
            Windy_remain_time_start = dic.ContainsKey("Windy_remain_time_start") ? Convert.ToInt32(dic["Windy_remain_time_start"]) : 0;
            PopularSeaPointId = dic.ContainsKey("PopularSeaPointId") ? Convert.ToInt32(dic["PopularSeaPointId"]) : 0;
            PopularProduceId = dic.ContainsKey("PopularProduceId") ? Convert.ToInt32(dic["PopularProduceId"]) : 0;
            PopularProduceRate = dic.ContainsKey("PopularProduceRate") ? Convert.ToInt32(dic["PopularProduceRate"]) : 0;
            PopularRemainTime = dic.ContainsKey("PopularRemainTime") ? Convert.ToSingle(dic["PopularRemainTime"]) : 0;
            PopularRemainTimeStart = dic.ContainsKey("PopularRemainTimeStart") ? Convert.ToInt32(dic["PopularRemainTimeStart"]) : 0;
            RefreshNum = dic.ContainsKey("RefreshNum") ? Convert.ToInt32(dic["RefreshNum"]) : 0;
            DataFlag = dic.ContainsKey("DataFlag") ? Convert.ToInt32(dic["DataFlag"]) : 0;
        }


        /// 新增数据
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <returns></returns>
        public int InsertData()
        {
            string sqlStr = @"insert into role(UID, Name, Gold, Food, SeaMan, WorkMan, Diamond, Level, Exp, Windy_dir, Windy_level, Windy_remain_time, Windy_remain_time_start, PopularSeaPointId, PopularProduceId, PopularProduceRate, PopularRemainTime, PopularRemainTimeStart,RefreshNum, DataFlag, CreateTime  )
                              values(?UID, ?Name, ?Gold, ?Food, ?SeaMan, ?WorkMan, ?Diamond, ?Level, ?Exp, ?Windy_dir, ?Windy_level, ?Windy_remain_time, ?Windy_remain_time_start, ?PopularSeaPointId, ?PopularProduceId, ?PopularProduceRate, ?PopularRemainTime, ?PopularRemainTimeStart,?RefreshNum, ?DataFlag, now() ); ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", this.UID),
                new MySqlParameter("?Name", this.Name),
                new MySqlParameter("?Gold", this.Gold),
                new MySqlParameter("?Food", this.Food),
                new MySqlParameter("?SeaMan", this.SeaMan),
                new MySqlParameter("?WorkMan", this.WorkMan),
                new MySqlParameter("?Diamond", Diamond),
                new MySqlParameter("?Level", this.Level),
                new MySqlParameter("?Exp", this.Exp),
                new MySqlParameter("?Windy_dir", this.Windy_dir),
                new MySqlParameter("?Windy_level", this.Windy_level),
                new MySqlParameter("?Windy_remain_time", this.Windy_remain_time),
                new MySqlParameter("?Windy_remain_time_start", this.Windy_remain_time_start),
                new MySqlParameter("?PopularSeaPointId", this.PopularSeaPointId),
                new MySqlParameter("?PopularProduceId", this.PopularProduceId),
                new MySqlParameter("?PopularProduceRate", this.PopularProduceRate),
                new MySqlParameter("?PopularRemainTime", this.PopularRemainTime),
                new MySqlParameter("?PopularRemainTimeStart", this.PopularRemainTimeStart),
                new MySqlParameter("?RefreshNum", this.RefreshNum),
                new MySqlParameter("?DataFlag", this.DataFlag)
            };
            //var ret = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            //return ret != null && ret != DBNull.Value ? Convert.ToInt32(ret) : 0;
            var ret = MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            return ret > 0 ? this.UID : 0;
        }

        /// 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public bool UpdateData()
        {
            string sqlStr = @"update role set Name=?Name, Gold=?Gold, Food=?Food, SeaMan=?SeaMan, WorkMan=?WorkMan, Diamond=?Diamond, Level=?Level, Exp=?Exp, Windy_dir=?Windy_dir, Windy_level=?Windy_level, Windy_remain_time=?Windy_remain_time, Windy_remain_time_start=?Windy_remain_time_start, 
                              PopularSeaPointId=?PopularSeaPointId, PopularProduceId=?PopularProduceId, PopularProduceRate=?PopularProduceRate, PopularRemainTime=?PopularRemainTime, PopularRemainTimeStart=?PopularRemainTimeStart, RefreshNum=?RefreshNum, DataFlag=?DataFlag
                              where UID=?UID ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", this.UID),
                new MySqlParameter("?Name", this.Name),
                new MySqlParameter("?Gold", this.Gold),
                new MySqlParameter("?Food", this.Food),
                new MySqlParameter("?SeaMan", this.SeaMan),
                new MySqlParameter("?WorkMan", this.WorkMan),
                new MySqlParameter("?Diamond", Diamond),
                new MySqlParameter("?Level", this.Level),
                new MySqlParameter("?Exp", this.Exp),
                new MySqlParameter("?Windy_dir", this.Windy_dir),
                new MySqlParameter("?Windy_level", this.Windy_level),
                new MySqlParameter("?Windy_remain_time", this.Windy_remain_time),
                new MySqlParameter("?Windy_remain_time_start", this.Windy_remain_time_start),
                new MySqlParameter("?PopularSeaPointId", this.PopularSeaPointId),
                new MySqlParameter("?PopularProduceId", this.PopularProduceId),
                new MySqlParameter("?PopularProduceRate", this.PopularProduceRate),
                new MySqlParameter("?PopularRemainTime", this.PopularRemainTime),
                new MySqlParameter("?PopularRemainTimeStart", this.PopularRemainTimeStart),
                new MySqlParameter("?RefreshNum", this.RefreshNum),
                new MySqlParameter("?DataFlag", this.DataFlag)
            };
            return MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param) > 0;
        }

        /// 查询
        /// <summary>
        /// 查询
        /// </summary>
        public bool SelectData(Dictionary<string, object> p)
        {
            bool result = false;
            string sqlStr = @"select UID, Name, Gold, Food, SeaMan, WorkMan, Diamond, Level, Exp, Windy_dir, Windy_level, Windy_remain_time, Windy_remain_time_start, PopularSeaPointId, PopularProduceId, PopularProduceRate, PopularRemainTime, PopularRemainTimeStart, RefreshNum, DataFlag, CreateTime
                              from role  where UID = ?UID limit 1";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
            };
            DataTable dt = MySQLDataControl.ExecuteDataTable(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            if (dt.Rows.Count > 0)
            {
                DRChangeToModel(dt.Rows[0]);
                result = true;
            }
            return result;
        }

        /// 查询列表数据的SQL语句
        /// <summary>
        /// 查询列表数据的SQL语句
        /// </summary>
        /// <returns></returns>
        public List<RoleData> GetDataList(Dictionary<string, object> p)
        {
            //            string sqlStr = @"select Id, UID, Name, Gold, Food, SeaMan, WorkMan, Diamond, Level, Windy_dir, Windy_remain_time, Windy_remain_time_start, PopularSeaPointId, PopularProduceId, PopularProduceRate, PopularRemainTime, PopularRemainTimeStart, DataFlag, CreateTime
            //                              from role  where UID = ?UID and DataFlag = 1 ";
            //            return sqlStr;
            return null;
        }

        /// datarow 转 model
        /// <summary>
        /// datarow 转 model
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public void DRChangeToModel(DataRow dr)
        {
            this.UID = Convert.ToInt32(dr["UID"]);
            this.Name = dr["Name"].ToString();
            this.Gold = Convert.ToInt32(dr["Gold"]);
            this.Food = Convert.ToInt32(dr["Food"]);
            this.SeaMan = Convert.ToInt32(dr["SeaMan"]);
            this.WorkMan = Convert.ToInt32(dr["WorkMan"]);
            this.Diamond = Convert.ToInt32(dr["Diamond"]);
            this.Level = Convert.ToInt32(dr["Level"]);
            this.Exp = Convert.ToInt32(dr["Exp"]);
            this.Windy_dir = Convert.ToInt32(dr["Windy_dir"]);
            this.Windy_level = Convert.ToInt32(dr["Windy_level"]);
            this.Windy_remain_time = Convert.ToInt32(dr["Windy_remain_time"]);
            this.Windy_remain_time_start = Convert.ToInt32(dr["Windy_remain_time_start"]);
            this.PopularSeaPointId = Convert.ToInt32(dr["PopularSeaPointId"]);
            this.PopularProduceId = Convert.ToInt32(dr["PopularProduceId"]);
            this.PopularProduceRate = Convert.ToInt32(dr["PopularProduceRate"]);
            this.PopularRemainTime = Convert.ToInt32(dr["PopularRemainTime"]);
            this.PopularRemainTimeStart = Convert.ToInt32(dr["PopularRemainTimeStart"]);
            this.RefreshNum = Convert.ToInt32(dr["RefreshNum"]);
            this.DataFlag = Convert.ToInt32(dr["DataFlag"]);
            this.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
        }

        /// 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public bool DeleteData(Dictionary<string, object> p)
        {
            string sqlStr = @"update role set DataFlag = 0  where UID = ?UID ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
            };
            return MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param) > 0;
        }

        /// 获取同一个账号下的数据个数
        /// <summary>
        /// 获取同一个账号下的数据个数
        /// </summary>
        /// <returns></returns>
        public int GetCount(Dictionary<string, object> p)
        {
            string sqlStr = @"select count(1) from role where UID = ?UID and DataFlag = 1 ";
            MySqlParameter[] param = new MySqlParameter[] { 
                new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
            };
            var ret = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            return ret != null && ret != DBNull.Value ? Convert.ToInt32(ret) : 0;
        }

        #endregion 方法

    }


}